package com.nd.mobile.study.view;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.nd.mobile.study.R;
import com.nd.mobile.study.common.App;
import com.nd.mobile.study.common.BaseActivity;
import com.nd.mobile.study.common.SimpleAsyncTask;
import com.nd.mobile.study.common.constants.BundleConstants;
import com.nd.mobile.study.common.exception.LocalErrorException;
import com.nd.mobile.study.common.util.StringUtils;
import com.nd.mobile.study.domain.HomeworkInfo;
import com.nd.mobile.study.domain.HomeworkQCatalog;
import com.nd.mobile.study.domain.Result;
import com.nd.mobile.study.view.helper.ToastHelper;


public class HomeworkInfoDetailActivity extends BaseActivity {

	private TextView			mTVTitle;
	private TextView			mTVContent;
	private HomeworkQCatalog	mHomeworkCatalog;
	private HomeworkInfo        mHomework;
	private Button 				mSubmit;
	private EditText			mEdtAnswer;

	@Override
	protected void onBaseCreate(Bundle savedInstanceState) {
		setContentView(R.layout.homework_detail);
	}

	@Override
	protected void initViews() {
		super.initHeader();
		mTVTitle = (TextView) findViewById(R.id.news_detail_subtitle);
		mTVContent = (TextView) findViewById(R.id.news_detail_content);
		mHeaderTitleTv.setText(R.string.tv_app_courseexercise);
		mHeaderBtnLogin.setVisibility(View.GONE);
		mSubmit = (Button) findViewById(R.id.hownwork_submit);
		mEdtAnswer = (EditText) findViewById(R.id.hownwork_answer);
		mHeaderBtnBack.setVisibility(View.VISIBLE);
		mHeaderBtnBack.setOnClickListener(new View.OnClickListener() {
		
			@Override
			public void onClick(View v) {
				//返回
				finish();
			}
		});
		mSubmit.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//提交答案
				submit();
			}
		});
		
		
	}
	
	protected void submit() {
		String answer = mEdtAnswer.getText().toString();
		if(StringUtils.isBlank(answer)){
			ToastHelper.displayToast(HomeworkInfoDetailActivity.this, "请先填写答案再提交");
		}else{
			mHomework.setAnswer(answer);
			mLoading.p();
			new SubmitAnswerTask(this).execute();
		}
		
		
	}

//	private void showNextContent(){
//		showDetail(mHomework);
//		
//			//最后一题
////			Intent mIntent = new Intent();
////			mIntent.setClass(HomeworkInfoDetailActivity.this,FinishedActivity.class);
////			mIntent.putExtra(BundleConstants.FINISH_INFO, "已完成本课程作业的内容");
////			startActivity(mIntent);
//		
//	}
	
	private void showHomework() {
		mTVTitle.setText(mHomework.getTitle());
		mTVContent.setText(Html.fromHtml(mHomework.getNote()));
		if(StringUtils.isBlank(mHomework.getAnswer())){
			mEdtAnswer.setText(mHomework.getAnswer());
		}
	}
	@Override
	protected void onAfterCreate(Bundle savedInstanceStat) {
		getIntentData();
		showDetail();
	}

	private void getIntentData() {
		mHomeworkCatalog = (HomeworkQCatalog) getIntent().getSerializableExtra(BundleConstants.HOMEWORK_INFO);
	}

	private void showDetail() {
		mLoading.p();
		new GetDetailTask(this).execute();
	}

	private class GetDetailTask extends SimpleAsyncTask<Void, Void, HomeworkInfo[]> {

		HomeworkInfo [] infos = null;
		
		public GetDetailTask(Context context) {
			super(context);
		}

		@Override
		protected HomeworkInfo[] onLoad(Void... params) throws Exception {
			try {
				infos = mHomeworkCatalog.getHomeworks(App.getInstance().getUser());
			} catch (LocalErrorException e) {
				e.printStackTrace();
				ToastHelper.displayToast(HomeworkInfoDetailActivity.this, e.getLocalizedMessage());
			} catch (Exception e) {
				e.printStackTrace();
				ToastHelper.displayToast(HomeworkInfoDetailActivity.this, "数据异常");

			}
			return infos;
		}

		@Override
		protected void onUI(HomeworkInfo[] detail) {
			if(detail!=null&&detail.length>0){
				mHomework = detail[0];
				showHomework();
			}else{
				ToastHelper.displayToast(HomeworkInfoDetailActivity.this, "数据异常");
			}
			mLoading.v();
		}

	}
	
	private class SubmitAnswerTask extends SimpleAsyncTask<Void, Void, Result> {
		
		public SubmitAnswerTask(Context context) {
			super(context);
		}

		@Override
		protected Result onLoad(Void... params) throws Exception {
			try {
				return mHomework.sendHomeworkAnser(App.getInstance().getUser());
			} catch (LocalErrorException e) {
				e.printStackTrace();
				ToastHelper.displayToast(HomeworkInfoDetailActivity.this, e.getLocalizedMessage());
			} catch (Exception e) {
				e.printStackTrace();
				ToastHelper.displayToast(HomeworkInfoDetailActivity.this, "数据异常");

			}
			return null;
		}

		@Override
		protected void onUI(Result detail) {
			if(detail!=null){
				if(Result.HOMEWORK_SUCCESS.equals(detail.getBody())){
					AlertDialog dialog = new AlertDialog.Builder(HomeworkInfoDetailActivity.this)
					.setTitle("答案提交成功")
					.setNeutralButton("返回列表",new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(DialogInterface dialog, int which) {
							finish();
						}
					})
					.setNegativeButton("我知道了", new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(DialogInterface dialog, int which) {
							
						}
					})
					.create();
					dialog.show();
				}else{
					ToastHelper.displayToast(HomeworkInfoDetailActivity.this,"<"+detail.getBody()+">");
				}
			}else{
				ToastHelper.displayToast(HomeworkInfoDetailActivity.this, "未提交成功");
			}
			mLoading.v();
		}

	}
}
